/*
 * xAPBSCmessage.java
 *
 * Java class used to store incoming xAP BSC messages
 *
 * Copyright (c) 2009 Daniel Berenguer <dberenguer@usapiens.com>
 *
 * This file is part of the jxAP project.
 *
 * OPNODE is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * OPNODE is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with canTracer; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * 12/08/09 by Daniel Berenguer : first version
 */
package xAP;

/********************************************************************************************
 * xAPBSCmessage class
 *
 * Class used to store xAP BSC message data. Create a new xAPmessage object whenever a new xAP
 * message is received.
 *
 *********************************************************************************************/
public class xAPBSCmessage extends xAPmessage
{
   /********************************************************************************************
    * Public member variables
    ********************************************************************************************/
    // Body fields
    public String title = "";       // Body title
    public xAPBSCstate state;       // ON/OFF BSC state
    public xAPBSCtext text;         // BSC text value
    public xAPBSClevel level;       // BSC level
    public short id = 0;            // BSC id field

    // Other data
    public String hbody;           // Body header (tittle)

   /********************************************************************************************
    * xAPBSCmessage
    *
    * Class constructor
    *
    * 'message'     xAP message string
    *
    ********************************************************************************************/
    public xAPBSCmessage(xAPmessage xAPdata)
    {        
        if (xAPdata != null)
        {
            // Copy header contents
            version = xAPdata.version;
            hop = xAPdata.hop;
            uid = xAPdata.uid;
            xClass = xAPdata.xClass;
            source = xAPdata.source;
            target = xAPdata.target;

            // Read body contents
            try
            {
                xAPBSCreadBody(xAPdata.body);
            }
            catch(xAPexception e)
            {
                e.display();
            }
        }
    }

    // Simple constructor

    public xAPBSCmessage()
    {

    }

   /********************************************************************************************
    * xAPBSCreadBody
    *
    * Read xAP BSC message body and fill class members
    *
    * 'body'     xAP BSC body string
    *
    ********************************************************************************************/
    private void xAPBSCreadBody(String body) throws xAPexception
    {
        int intStart, intEnd;
        String strBodyCont;

        // Store the header of the body
        if ((intEnd = body.indexOf('\n')) == -1)
            throw new xAPexception("Message body malformed");

        // Body header (tittle)
        hbody = body.substring(0, intEnd);

        // Reach the beginning of the body contents
        if ((intStart = body.indexOf("\n{\n")) == -1)
            throw new xAPexception("Message body malformed");

        // Get body header string
        title = body.substring(intStart);
        intStart += 3;

        // Reach the end of the body contents
        if ((intEnd = body.indexOf("}")) == -1)
            throw new xAPexception("Message body malformed");

        // Store body contents
        strBodyCont = body.substring(intStart, intEnd);

        // Extract xAP BSC data from body

        // Get BSC state
        state = xAPBSCgetState(strBodyCont);
        // Get BSC text
        text = xAPBSCgetText(strBodyCont);
        // Get BSC level
        level = xAPBSCgetLevel(strBodyCont);
        // Get id
        id = xAPBSCgetId(strBodyCont);
    }

   /********************************************************************************************
    * xAPBSCgetState
    *
    * Get BSC state from message body
    *
    * 'bodyCont'        Body contents
    *
    *  Return:
    *   BSC State
    ********************************************************************************************/
    private xAPBSCstate xAPBSCgetState(String bodyCont)
    {
        int intStart = 0, intEnd;
        String strKey;

        while ((intEnd = bodyCont.indexOf('=', intStart)) != -1)
        {
            // Get keyword
            strKey = bodyCont.substring(intStart, intEnd);

            intStart = intEnd + 1;
            intEnd = bodyCont.indexOf('\n', intStart);
            if (strKey.compareToIgnoreCase("state") == 0)
            {
                xAPBSCstate bscState = new xAPBSCstate(bodyCont.substring(intStart, intEnd));
                return bscState;
            }
            intStart = intEnd + 1;
        }

        return null;
    }

   /********************************************************************************************
    * xAPBSCgetText
    *
    * Get BSC text from message body
    *
    * 'bodyCont'        Body contents
    *
    *  Return:
    *   BSC Text
    ********************************************************************************************/
    private xAPBSCtext xAPBSCgetText(String bodyCont)
    {
        int intStart = 0, intEnd;
        String strKey;

        while ((intEnd = bodyCont.indexOf('=', intStart)) != -1)
        {
            // Get keyword
            strKey = bodyCont.substring(intStart, intEnd);

            intStart = intEnd + 1;
            intEnd = bodyCont.indexOf('\n', intStart);
            if (strKey.compareToIgnoreCase("text") == 0)
            {
                xAPBSCtext bscText = new xAPBSCtext(bodyCont.substring(intStart, intEnd));
                return bscText;
            }

            intStart = intEnd + 1;
        }

        return null;
    }

/********************************************************************************************
    * xAPBSCgetLevel
    *
    * Get BSC level from message body
    *
    * 'bodyCont'        Body contents
    *
    *  Return:
    *   BSC level
    ********************************************************************************************/
    private xAPBSClevel xAPBSCgetLevel(String bodyCont)
    {
        int intStart = 0, intEnd;
        String strKey;

        while ((intEnd = bodyCont.indexOf('=', intStart)) != -1)
        {
            // Get keyword
            strKey = bodyCont.substring(intStart, intEnd);

            intStart = intEnd + 1;
            intEnd = bodyCont.indexOf('\n', intStart);
            if (strKey.compareToIgnoreCase("level") == 0)
                return new xAPBSClevel(bodyCont.substring(intStart, intEnd));
            intStart = intEnd + 1;
        }

        return null;
    }

   /********************************************************************************************
    * xAPBSCgetId
    *
    * Get id field from message body
    *
    * 'bodyCont'        Body contents
    *
    *  Return:
    *   id number
    *   -1 for id=*
    *   -2 unsupported id or id not found
    ********************************************************************************************/
    private short xAPBSCgetId(String bodyCont)
    {
        int intStart = 0, intEnd;
        String strKey, strId;

        while ((intEnd = bodyCont.indexOf('=', intStart)) != -1)
        {
            // Get keyword
            strKey = bodyCont.substring(intStart, intEnd);

            intStart = intEnd + 1;
            intEnd = bodyCont.indexOf('\n', intStart);
            if (strKey.compareToIgnoreCase("id") == 0)
            {
                strId = bodyCont.substring(intStart, intEnd);
                if (strId.compareTo("*") == 0)
                    return -1;

                return Short.parseShort(strId);
            }
            intStart = intEnd + 1;
        }

        return -2;
    }

   /********************************************************************************************
    * setTarget
    *
    * Set target address for the current xAP BSC message
    *
    * 'strTarget' target address
    *
    ********************************************************************************************/
    public void setTarget(String strTarget)
    {
        target = strTarget;
    }

   /********************************************************************************************
    * setSource
    *
    * Set source address for the current xAP BSC message
    *
    * 'strSource' source address
    *
    ********************************************************************************************/
    public void setSource(String strSource)
    {
        source = strSource;
    }

   /********************************************************************************************
    * setUID
    *
    * Set UID for the current xAP BSC message
    *
    * 'strUID' UID string
    *
    ********************************************************************************************/
    public void setUID(String strUID)
    {
        uid = strUID;
    }
}